﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PO_2011.ColorConvert
{
    class Colors
    {
        public static Bitmap convertBitmapColorToGreyOrBlack(Bitmap image, int type)
        {
            if(image.PixelFormat == PixelFormat.Format32bppArgb)
            {
                Bitmap image_grey = new Bitmap(image.Width, image.Height);
                Color color;
                int color_char;

                for (int y = 0; y < image.Height; y++)
                {
                    for (int x = 0; x < image.Width; x++)
                    {
                        if (type == 1)
                        {
                            color_char = (int) convertFromColorToGray(image.GetPixel(x, y));
                            color = Color.FromArgb(color_char, color_char, color_char);
                            image_grey.SetPixel(x, y, color);
                        }
                    }
                }

                return image_grey;
            }
            else
            {
                return image;
            }
        }

        public static char convertFromColorToBlack(Color color, String new_type, String old_type)
        {
            char one_color;
            int black = 90;
            if ((new_type == old_type) | (new_type == "P1" && old_type == "P4"))
            {
                one_color = (Char)color.R == '1' ? '1' : '0';
            }
            else if (old_type == "P2" | old_type == "P5")
            {
                one_color = (int)color.R < black ? '1' : '0';
            }
            else
            {
                one_color = (int)convertFromColorToGray(color, new_type, old_type) < black + 40 ? '1' : '0';
            }

            return one_color;
        }

        public static char convertFromColorToGray(Color color, String new_type = "", String old_type = "")
        {
            return (char)(0.21 * color.R + 0.71 * color.G + 0.07 * color.B);
        }



    }
}
